home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / CRCUTIL.ZIP / CRCCHK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-15  |  5.0 KB  |  145 lines

  1. /***********************************************************************
  2. *
  3. *  CRCCHK - A simple program to check the correct functioning of
  4. *           the CRC utility routines. This program calculates the CRC
  5. *           of the specified file. CRCs are calculated least significant
  6. *           bit first using both the slow, memory efficient algorithm
  7. *           and the faster table lookup algorithm.
  8. *
  9. *  Arguments: File Name   - The name of the file to be checked.
  10. *
  11. ***********************************************************************/
  12. #include <stdio.h>
  13. #include "crcutil.h"
  14.  
  15. /* The coefficient of each term of the generator polynomial is represented
  16.    by a bit in the polynomial array (except for the highest order term
  17.    which is always assumed to be 1). The coefficient for the second
  18.    highest term in the LSB of the first byte of the polynomial array. */
  19.  
  20. /***********************************************************************
  21. *  CCITT polynomial (Sec 2.2.7 Rec. X.25 1984):
  22. *    Polynomial   x^16 + x^12 + x^5 + 1
  23. *    Generator is pre-initialized to 0xffff
  24. *    CRC is inverted before transmission on link.
  25. *  CCITT examples from Appendix 1, Rec. X.25:
  26. *    Note: bytes are MSB to LSB and are transmitted LSB
  27. *    first (FCSs do not include 0 bits inserted for
  28. *    transparency).
  29. *    eg. byte 03 would be transmitted as 11000000
  30. *                                        Time ->
  31. *
  32. *    Data    FCS
  33. *
  34. *    03 3f   5b ec
  35. *    01 73   83 57
  36. *    01 3f   eb df
  37. *    03 73   33 64
  38. ***********************************************************************/
  39. CRC16 ccitt_tbl[256];
  40. CRC16 poly_ccitt = {0x8408};
  41. CRC16 crc_ccitt = {0xffff};
  42. CRC16 Scrc_ccitt = {0xffff};
  43.  
  44.                                               
  45. /***********************************************************************
  46. *  CRC16 (Bisync)
  47. *    Polynomial   x^16 + x^15 + x^2 + 1
  48. *    Generator is pre-initialized to 0x0000
  49. *    CRC is transmitted as is on link.
  50. ***********************************************************************/
  51. CRC16 crcbsc_tbl[256];
  52. CRC16 poly_crcbsc = {0xa001};
  53. CRC16 crc_crcbsc = {0x0000};
  54. CRC16 Scrc_crcbsc = {0x0000};
  55.  
  56.  
  57. /***********************************************************************
  58. *  Ethernet CRC
  59. *    Polynomial   x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 +
  60. *                       x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
  61. *    Generator is pre-initialized to 0xFFFFFFFF
  62. *    CRC is inverted before transmission on link. ???
  63. *
  64. *  I have no data to verify that the generation of the Ethernet CRC is
  65. *  correct (yet). Some day I will get a line trace with real data to
  66. *  verify this program.
  67. ***********************************************************************/
  68. CRC32 ether32_tbl[256];
  69. CRC32 poly_ether32 = {0xedb88320};
  70. CRC32 crc_ether32 = {0xffffffff};
  71. CRC32 Scrc_ether32 = {0xffffffff};
  72.  
  73. FILE *fp;
  74. char buff[8192];
  75.  
  76. main(argc, argv)
  77.    int argc;
  78.    char *argv[];
  79. {
  80.    unsigned len,i,c;
  81.    unsigned hi_byte_cnt,lo_byte_cnt;
  82.  
  83.    /* We expect 1 argument */
  84.  
  85.    if (argc < 2)
  86.    {
  87.       printf("Usage: %s file\n", argv[0]);
  88.       exit(1);
  89.    }
  90.    fp=fopen(argv[1],"rb");
  91.  
  92.    /* Set up  CRC tables for lookup method */
  93.    I_CRC16(ccitt_tbl, &poly_ccitt);
  94.    I_CRC16(crcbsc_tbl, &poly_crcbsc);
  95.    I_CRC32(ether32_tbl, &poly_ether32);
  96.  
  97.    lo_byte_cnt = 0;
  98.    hi_byte_cnt = 0;
  99.  
  100.    /* Read input file in blocks over which the CRC is calculated since
  101.       this is more efficient. */
  102.    while((len = fread(buff, 1, sizeof(buff), fp)) != 0)
  103.    {
  104.       F_CRC16(ccitt_tbl, &crc_ccitt, buff, len);
  105.       F_CRC16(crcbsc_tbl, &crc_crcbsc, buff, len);
  106.       F_CRC32(ether32_tbl, &crc_ether32, buff, len);
  107.       S_CRC16(&poly_ccitt, &Scrc_ccitt, buff, len);
  108.       S_CRC16(&poly_crcbsc, &Scrc_crcbsc, buff, len);
  109.       S_CRC32(&poly_ether32, &Scrc_ether32, buff, len);
  110.  
  111.       /* Byte count of file (up to 65M) */
  112.  
  113.       if((lo_byte_cnt += len) >= 10000)
  114.       {
  115.          ++hi_byte_cnt;
  116.          lo_byte_cnt -= 10000;
  117.       }
  118.    }
  119.    fclose(fp);
  120.  
  121.    printf("\n%s completed:\n", argv[0]);
  122.    printf(" bytes       = %d%04d\n",hi_byte_cnt,lo_byte_cnt);
  123.    printf(" CRC16 CRC   Fast = 0x%04x  Slow = 0x%04x\n", crc_crcbsc, Scrc_crcbsc);
  124.    printf(" CCITT CRC   Fast = 0x%04x  Slow = 0x%04x\n", crc_ccitt, Scrc_ccitt);
  125.    printf(" Ether32 CRC   Fast = 0x%08lx  Slow = 0x%08lx\n", crc_ether32,  Scrc_ether32);
  126.  
  127.    printf("\nFor data comm transmission:\n");
  128.    printf(" CRC16 CRC  byte(1) 0x%02x, byte(2) 0x%02x\n", 
  129.           crc_crcbsc & 0xff, 
  130.           crc_crcbsc>>8);
  131.    /* These CRCs are inverted before transmision in data comm */
  132.    crc_ccitt ^= 0xffff;
  133.    crc_ether32 ^= 0xffffffff;
  134.    printf(" CCITT CRC  byte(1) 0x%02x, byte(2) 0x%02x\n", 
  135.           crc_ccitt & 0xff, 
  136.           (crc_ccitt>>8) & 0xff);
  137.    printf(" Ether32 CRC  byte(1) 0x%02lx, byte(2) 0x%02lx, byte(3) 0x%02lx, byte(4) 0x%02lx\n", 
  138.           crc_ether32 & 0xff, 
  139.           (crc_ether32>>8) & 0xff, 
  140.           (crc_ether32>>16) & 0xff, 
  141.           (crc_ether32>>24) & 0xff);
  142.    exit(0);
  143.    return 0; /* To keep compiler happy */
  144. }
  145.